package cn.iocoder.yudao.module.written.exam.controller.admin.userwrittenexamrecordsdetail;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.written.exam.dal.dataobject.userwrittenexamrecords.UserWrittenExamRecordsDO;
import cn.iocoder.yudao.module.written.exam.service.userwrittenexamrecords.UserWrittenExamRecordsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.written.exam.controller.admin.userwrittenexamrecordsdetail.vo.*;
import cn.iocoder.yudao.module.written.exam.dal.dataobject.userwrittenexamrecordsdetail.UserWrittenExamRecordsDetailDO;
import cn.iocoder.yudao.module.written.exam.service.userwrittenexamrecordsdetail.UserWrittenExamRecordsDetailService;

@Tag(name = "管理后台 - 笔试记录详情")
@RestController
@RequestMapping("/written-exam/user-written-exam-records-detail")
@Validated
public class UserWrittenExamRecordsDetailController {

    @Value("${zhima.image-prefix-url:}")
    private String imagePrefixUrl;

    @Resource
    private UserWrittenExamRecordsService userWrittenExamRecordsService;
    @Resource
    private UserWrittenExamRecordsDetailService userWrittenExamRecordsDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建笔试记录详情")
    @PreAuthorize("@ss.hasPermission('written-exam:user-written-exam-records-detail:create')")
    public CommonResult<Long> createUserWrittenExamRecordsDetail(@Valid @RequestBody UserWrittenExamRecordsDetailSaveReqVO createReqVO) {
        return success(userWrittenExamRecordsDetailService.createUserWrittenExamRecordsDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新笔试记录详情")
    @PreAuthorize("@ss.hasPermission('written-exam:user-written-exam-records-detail:update')")
    public CommonResult<Boolean> updateUserWrittenExamRecordsDetail(@Valid @RequestBody UserWrittenExamRecordsDetailSaveReqVO updateReqVO) {
        userWrittenExamRecordsDetailService.updateUserWrittenExamRecordsDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除笔试记录详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('written-exam:user-written-exam-records-detail:delete')")
    public CommonResult<Boolean> deleteUserWrittenExamRecordsDetail(@RequestParam("id") Long id) {
        userWrittenExamRecordsDetailService.deleteUserWrittenExamRecordsDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得笔试记录详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('written-exam:user-written-exam-records-detail:query')")
    public CommonResult<UserWrittenExamRecordsDetailRespVO> getUserWrittenExamRecordsDetail(@RequestParam("id") Long id) {
        UserWrittenExamRecordsDetailDO userWrittenExamRecordsDetail = userWrittenExamRecordsDetailService.getUserWrittenExamRecordsDetail(id);
        return success(BeanUtils.toBean(userWrittenExamRecordsDetail, UserWrittenExamRecordsDetailRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得笔试记录详情分页")
    @PreAuthorize("@ss.hasPermission('written-exam:user-written-exam-records-detail:query')")
    public CommonResult<PageResult<UserWrittenExamRecordsDetailRespVO>> getUserWrittenExamRecordsDetailPage(@Valid @RequestBody UserWrittenExamRecordsDetailPageReqVO pageReqVO) {
        PageResult<UserWrittenExamRecordsDetailDO> pageResult = userWrittenExamRecordsDetailService.getUserWrittenExamRecordsDetailPage(pageReqVO);
        PageResult<UserWrittenExamRecordsDetailRespVO> pageResultResp = new PageResult<>();
        pageResultResp.setTotal(pageResult.getTotal());
        List<UserWrittenExamRecordsDetailRespVO> userWrittenExamRecordsDetailRespVOList = new ArrayList<>();
        pageResult.getList().forEach(userWrittenExamRecordsDetail -> {
            UserWrittenExamRecordsDetailRespVO userWrittenExamRecordsDetailRespVO = BeanUtils.toBean(userWrittenExamRecordsDetail, UserWrittenExamRecordsDetailRespVO.class);

            UserWrittenExamRecordsDO userWrittenExamRecordsDO = userWrittenExamRecordsService.getUserWrittenExamRecords(userWrittenExamRecordsDetail.getRecordsId());
            if (Objects.nonNull(userWrittenExamRecordsDO)) {
                userWrittenExamRecordsDetailRespVO.setConversationId(userWrittenExamRecordsDO.getConversationId());
            }

            userWrittenExamRecordsDetailRespVO.setFileIds(CollectionUtils.convertStringToList(userWrittenExamRecordsDetail.getFileIds(), String.class));
            List<String> fileUrlsAddPrefix = new ArrayList<>();
            List<String> fileUrls = CollectionUtils.convertStringToList(userWrittenExamRecordsDetail.getFileUrls(), String.class);
            for (String fileUrl : fileUrls) {
                fileUrlsAddPrefix.add(imagePrefixUrl + fileUrl);
            }
            userWrittenExamRecordsDetailRespVO.setFileUrls(fileUrlsAddPrefix);
            userWrittenExamRecordsDetailRespVOList.add(userWrittenExamRecordsDetailRespVO);
        });
        pageResultResp.setList(userWrittenExamRecordsDetailRespVOList);

        return success(pageResultResp);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出笔试记录详情 Excel")
    @PreAuthorize("@ss.hasPermission('written-exam:user-written-exam-records-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserWrittenExamRecordsDetailExcel(@Valid UserWrittenExamRecordsDetailPageReqVO pageReqVO,
                                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserWrittenExamRecordsDetailDO> list = userWrittenExamRecordsDetailService.getUserWrittenExamRecordsDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "笔试记录详情.xls", "数据", UserWrittenExamRecordsDetailRespVO.class,
                BeanUtils.toBean(list, UserWrittenExamRecordsDetailRespVO.class));
    }

}